<?php

namespace app\Admin\controller;

use think\Controller;
use think\Request;
use think\Validate;

class Goods extends Base
{
    public function search(){
        $type = input("goods_type");
        $where = [];
        if(!empty($type)){
            $where['goods_type'] = $type;
        }
        $data = \app\Admin\model\Good::where($where)->paginate(3,false,['query'=>['goods_type'=>$type]]);
        return  view('show',['data'=>$data]);
    }

    public function cut($url){
        $image = \think\Image::open($url);
       //将图片裁剪为50x50并保存
        $img = "uploads/crop".rand(100,99).".jpg";
       $image->crop(50, 50)->save($img);
        return   redirect("/goodss");
    }



    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $good = new \app\Admin\model\Good();
        $data = $good->getAll();
        return  view('show',['data'=>$data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('add');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->except("/goods");
//        进行validate验证
        $rules = [
            "goods_name"=>"require",
            "count"=>"require",
            "price"=>"require",
            "number"=>"require",
            "goods_type"=>"require",
            "business"=>"require",
            "addtime"=>"require"
        ];
        $vail= new Validate($rules);
        $res = $vail->check($data);
        if($res != true){
            dump("表单验证错误");die();
        }
        $file = request()->file('pic');
//        文件大小小于15000
    $info = $file->validate(['size'=>15000])->move(ROOT_PATH . 'public' . DS . 'uploads');
   if($info){
       $img = "uploads/".$info->getSaveName();
       $image = \think\Image::open($img);
      // 按照原图的比例生成一个最大为100*100的缩略图并保存
       $real_img = substr($img,0,strrpos($img,".")).rand(100,999).".jpg";
       $image->thumb(100, 100)->save($real_img);
//       数据库保存的是略缩后的图片
       $data['pic'] = $real_img;
    }else{
          // 上传失败获取错误信息
       echo $file->getError(); die();
   }
      $good = new \app\Admin\model\Good();
      $res = $good->addGood($data);
      if($res){
       return   redirect("/goodss");
      }else{
       return   redirect("/goods/create");
      }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $good = new \app\Admin\model\Good();
        $res = $good->delGood($id);
        if($res){
            return   json(['code'=>200,'data'=>'','msg'=>'删除成功']);
        }else{
            return   json(['code'=>400,'data'=>'','msg'=>'删除失败']);
        }
    }
}
